package com.unittec.drainage.provider.manage.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unittec.drainage.provider.excel.export.ExportExcelColumn;
import com.unittec.drainage.provider.excel.export.ExportExcelUtil;
import com.unittec.drainage.provider.log.Log;
import com.unittec.drainage.provider.manage.entity.WaterheadUser;
import com.unittec.drainage.provider.manage.dto.WaterheadUserDto;
import com.unittec.drainage.provider.manage.service.RegionService;
import com.unittec.drainage.provider.manage.service.WaterheadUserService;
import com.unittec.drainage.provider.utils.CopyUtils;
import com.unittec.drainage.provider.utils.ValidationUtils;
import com.unittec.drainage.sdk.log.enums.LogOperateEnum;
import com.unittec.drainage.sdk.log.enums.LogStatusEnum;
import com.unittec.drainage.sdk.manage.enums.WaterheadUserLevyStandardEnum;
import com.unittec.drainage.sdk.manage.enums.WaterheadUserPriceCategoryEnum;
import com.unittec.drainage.sdk.manage.enums.WaterheadUserStatusEnum;
import com.unittec.drainage.sdk.manage.enums.WaterheadUserWaterNatureEnum;
import com.unittec.drainage.sdk.manage.request.*;
import com.unittec.drainage.sdk.manage.response.*;
import com.unittec.drainage.sdk.root.domain.GatewayRequest;
import com.unittec.drainage.sdk.root.domain.Result;
import com.unittec.drainage.sdk.root.enums.BizCode;
import com.unittec.drainage.sdk.root.enums.BizCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 水源用户管理
 *
 * @author wangyongfeng
 * @date 2021/8/16
 */
@RestController
@Api(tags = "水源用户管理")
public class WaterheadUserController {

    @Autowired
    private Log log;

    @Resource
    private WaterheadUserService waterheadUserService;

    @Resource
    private RegionService regionService;

    //新增水源用户
    @RequestMapping(value = "/301100", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增水源用户", notes = "新增水源用户")
    public Result<Res301100> addWaterheadUser(@RequestBody GatewayRequest<Req301100> req) {
        log.debug(req, "addWaterheadUser {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301100 res301100 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            waterheadUserService.addWaterheadUser(req.getContent());
            res301100 = new Res301100();
            res301100.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301100", JSON.toJSONString(req), "新增水源用户", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "addWaterheadUser {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301100", JSON.toJSONString(req), "新增水源用户", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301100);
        } else {
            log.addOperateLog("301100", JSON.toJSONString(req), "新增水源用户", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    //水源用户编辑
    @RequestMapping(value = "/301101", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "水源用户编辑", notes = "水源用户编辑")
    public Result<Res301101> updateWaterheadUser(@RequestBody GatewayRequest<Req301101> req) {
        log.debug(req, "updateWaterheadUser {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301101 res301101 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            waterheadUserService.updateWaterheadUser(req.getContent());
            res301101 = new Res301101();
            res301101.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301101", JSON.toJSONString(req), "水源用户编辑", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "updateWaterheadUser {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301101", JSON.toJSONString(req), "水源用户编辑", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301101);
        } else {
            log.addOperateLog("301101", JSON.toJSONString(req), "水源用户编辑", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //水源用户销户
    @RequestMapping(value = "/301102", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "水源用户销户", notes = "水源用户销户")
    public Result<Res301102> delWaterheadUser(@RequestBody GatewayRequest<Req301102> req) {
        log.debug(req, "delWaterheadUser {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301102 res301102 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            waterheadUserService.delWaterheadUser(req.getContent());
            res301102 = new Res301102();
            res301102.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301102", JSON.toJSONString(req), "水源用户销户", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "delWaterheadUser {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301102", JSON.toJSONString(req), "水源用户销户", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301102);
        } else {
            log.addOperateLog("301102", JSON.toJSONString(req), "水源用户销户", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    //水源用户详情
    @RequestMapping(value = "/301103", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "水源用户详情", notes = "水源用户详情")
    public Result<Res301103> getWaterheadUserInfo(@RequestBody GatewayRequest<Req301103> req) {
        log.debug(req, "getWaterheadUserInfo {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301103 res301103 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            WaterheadUserDto waterheadUserDto = waterheadUserService.getWaterheadUserInfo(req.getContent());
            res301103 = new Res301103();
            res301103.setWaterheadUser(waterheadUserDto);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301103", JSON.toJSONString(req), "水源用户详情", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getWaterheadUserInfo {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301103", JSON.toJSONString(req), "水源用户详情", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301103);
        } else {
            log.addOperateLog("301103", JSON.toJSONString(req), "水源用户详情", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    //水源用户导出
    @RequestMapping(value = "/301104", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "导出水源用户", notes = "导出水源用户")
    public Result<?> exportWaterheadUserExcel(@RequestBody GatewayRequest<Req301104> req, HttpServletResponse response) {
        log.debug(req, "exportWaterheadUserExcel {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        try {
            ValidationUtils.validateEntityStr(req.getContent());

            //获取需要转出的excel表头的map字段
            List<ExportExcelColumn> headers = new ArrayList<>();
            headers.add(new ExportExcelColumn("user_code","用户代码"));
            headers.add(new ExportExcelColumn("user_name","用户名称"));
            headers.add(new ExportExcelColumn("user_address","用户地址"));
            headers.add(new ExportExcelColumn("user_phone","联系电话"));
            headers.add(new ExportExcelColumn("levy_mode","征收方式"));
            Map<String, String> levyStandardMap = new HashMap<>(16);
            levyStandardMap.put(WaterheadUserLevyStandardEnum.levy_standard_0.getKey(), WaterheadUserLevyStandardEnum.levy_standard_0.getValue());
            levyStandardMap.put(WaterheadUserLevyStandardEnum.levy_standard_1.getKey(), WaterheadUserLevyStandardEnum.levy_standard_1.getValue());
            headers.add(new ExportExcelColumn("levy_standard","征收标准", true, levyStandardMap));
            headers.add(new ExportExcelColumn("water_consumption","用水量(m³)"));
            headers.add(new ExportExcelColumn("water_discharge","排水量(m³)"));
            Map<String, String> priceCategoryMap = new HashMap<>(16);
            priceCategoryMap.put(WaterheadUserPriceCategoryEnum.price_category_0.getKey(), WaterheadUserPriceCategoryEnum.price_category_0.getValue());
            priceCategoryMap.put(WaterheadUserPriceCategoryEnum.price_category_1.getKey(), WaterheadUserPriceCategoryEnum.price_category_1.getValue());
            headers.add(new ExportExcelColumn("price_category","污水价类别", true, priceCategoryMap));
            Map<String, String> waterNatureMap = new HashMap<>(16);
            waterNatureMap.put(WaterheadUserWaterNatureEnum.water_nature_1.getKey(), WaterheadUserWaterNatureEnum.water_nature_1.getValue());
            waterNatureMap.put(WaterheadUserWaterNatureEnum.water_nature_2.getKey(), WaterheadUserWaterNatureEnum.water_nature_2.getValue());
            waterNatureMap.put(WaterheadUserWaterNatureEnum.water_nature_3.getKey(), WaterheadUserWaterNatureEnum.water_nature_3.getValue());
            waterNatureMap.put(WaterheadUserWaterNatureEnum.water_nature_4.getKey(), WaterheadUserWaterNatureEnum.water_nature_4.getValue());
            waterNatureMap.put(WaterheadUserWaterNatureEnum.water_nature_5.getKey(), WaterheadUserWaterNatureEnum.water_nature_5.getValue());
            headers.add(new ExportExcelColumn("water_nature","用水性质", true, waterNatureMap));
            headers.add(new ExportExcelColumn("opne_time","立户日期"));
            Map<String, String> stateMap = new HashMap<>(16);
            stateMap.put(WaterheadUserStatusEnum.status_0.getKey(), WaterheadUserStatusEnum.status_0.getValue());
            stateMap.put(WaterheadUserStatusEnum.status_1.getKey(), WaterheadUserStatusEnum.status_1.getValue());
            headers.add(new ExportExcelColumn("state","用户状态", true, stateMap));
            headers.add(new ExportExcelColumn("region_name","水系"));
            headers.add(new ExportExcelColumn("river_system_name","行政区域"));

            new ExportExcelUtil<>().exportExcelForMap("水源用户列表", headers, waterheadUserService.getWaterheadUserList(req.getContent()), ExportExcelUtil.EXCEl_FILE_2007, response);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301104", JSON.toJSONString(req), "导出水源用户", log.buildErrmsg(e),
                    LogOperateEnum.LOG_EXPORT.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "exportWaterheadUserExcel {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301104", JSON.toJSONString(req), "导出水源用户", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_EXPORT.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req);
        } else {
            log.addOperateLog("301104", JSON.toJSONString(req), "导出水源用户", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_EXPORT.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
        }
        return Result.error(req, bizCodeEnum);
    }
    
    //分页条件查询用户
    @RequestMapping(value = "/301105", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分页条件查询用户", notes = "分页条件查询用户")
    public Result<Res301105> getWaterheadUserPage(@RequestBody GatewayRequest<Req301105> req) {
        log.debug(req, "getWaterheadUserPage {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301105 res301105 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            IPage pageList  = waterheadUserService.getWaterheadUserPage(req.getContent());
            res301105 = new Res301105();
            res301105.setPage(pageList);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301105", JSON.toJSONString(req), "分页条件查询用户", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getWaterheadUserPage {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301105", JSON.toJSONString(req), "分页条件查询用户", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301105);
        } else {
            log.addOperateLog("301105", JSON.toJSONString(req), "分页条件查询用户", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    //自动生成用户代码
    @RequestMapping(value = "/301121", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "自动生成用户代码", notes = "自动生成用户代码")
    public Result<Res301121> autoGenWaterheadUserCode(@RequestBody GatewayRequest<Req301121> req) {
        log.debug(req, "autoGenWaterheadUserCode {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301121 res301121 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            String rs = waterheadUserService.autoGenWaterheadUserCode(req.getContent());
            res301121 = new Res301121();
            res301121.setCode(rs);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301121", JSON.toJSONString(req), "自动生成用户代码", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "autoGenWaterheadUserCode {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301121", JSON.toJSONString(req), "自动生成用户代码", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301121);
        } else {
            log.addOperateLog("301121", JSON.toJSONString(req), "自动生成用户代码", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    //根据用户代码查看用户详情
    @RequestMapping(value = "/301122", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据用户代码查看用户详情", notes = "根据用户代码查看用户详情")
    public Result<Res301122> getWaterheadUserByCode(@RequestBody GatewayRequest<Req301122> req) {
        log.debug(req, "getWaterheadUserByCode {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301122 res301122 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            WaterheadUser w = waterheadUserService.getWaterheadUserByCode(req.getContent());
            res301122 = new Res301122();
            if(null!=w){
                CopyUtils.copyProperties(w,res301122);
            }
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301122", JSON.toJSONString(req), "根据用户代码查看用户详情", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getWaterheadUserByCode {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301122", JSON.toJSONString(req), "根据用户代码查看用户详情", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301122);
        } else {
            log.addOperateLog("301122", JSON.toJSONString(req), "根据用户代码查看用户详情", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    //用户代码模糊查询
    @RequestMapping(value = "/301124", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "用户代码模糊查询", notes = "用户代码模糊查询")
    public Result<Res301124> getCodeByCode(@RequestBody GatewayRequest<Req301124> req) {
        log.debug(req, "getCodeByCode {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301124 res301124 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = waterheadUserService.getCodeByCode(req.getContent());
            res301124 = new Res301124();
            res301124.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301124", JSON.toJSONString(req), "用户代码模糊查询", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getCodeByCode {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301124", JSON.toJSONString(req), "用户代码模糊查询", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301124);
        } else {
            log.addOperateLog("301124", JSON.toJSONString(req), "用户代码模糊查询", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
}
